<?php
/**
 * 【审批流控制器】
 * Created by PhpStorm.
 * User: 孙凯
 * Date: 2018-06-05
 * Time: 15:00
 */

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Support\Facades\DB;
use  App\Http\Comm\CommonController as com;

class ApprovalController extends Controller
{

    /**
     * 【审批列表】
     * @Author 孙凯
     * @DateTime 2018-06-05
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        try {
            $where = [];
            if(isset($request['name'])){
                $where['name'] = $request['name'];
            }

           /* $starttime = strtotime($request['starttime']);
            $endtime = strtotime($request['endtime']);*/
            $handle = DB::table('approval')
//                ->join('roles', 'approval.approval_id', '=', 'roles.id')
                ->orderBy('approval.time','desc')
                ->get()->toarray();//日志表
            $datas = json_decode(json_encode($handle), true);
            foreach ($datas as $k=>&$v){
                $v['time'] = date('Y-m-d H:i:s', $v['time']);
            }
            $msg = json_decode(json_encode($datas));
            return $msg ? response(['msg' => $msg, 'message' => '审批流数据获取成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '审批流数据获取失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }
    }

    /**
     * 【审批流添加页面展示】
     * @Author sk
     * @DateTime 2018-06-05
     * @param Request $request
     * @return array
     */
    public function add(Request $request)
    {
        //$arr = $request->all();
        try {
            $type = Db::table('approval')->get()->toarray();
            $index = Db::table('roles')->select('id','pid','name as value')->get();
            $index = json_decode(json_encode($index),true);
            $com = new com;
            $newindex = $com->createTree($index);
            //dd($newindex);
            $msg = json_decode(json_encode($newindex));
           // dd($msg);
            $type = json_decode(json_encode($type));
            return $msg ? response(['msg' => $msg, 'type' => $type, 'message' => '审批流数据获取成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '审批流数据获取失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }

    }

    /**
     * 【审批流添加数据到数据库】
     * @param type string 类型
     * @param approval_id int 角色id
     * @param reset_person string 修改人
     * @Author sk
     * @DateTime 2018-06-05
     * @param Request $request
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     */
    public function addpost(Request $request)
    {
        $arr = $request->all();
        if (empty($arr['type'])||empty($arr['approval_id'])||empty($arr['reset_person'])){
            return '参数不全';
        }
        try {
            $id = DB::table('approval')->insertGetId(
                [
                    'type' => $arr['type'],
                    'approval_id' => $arr['approval_id'],
                    'reset_person' => $arr['reset_person'],
                    'time' => time(),
                ]);
            $msg =  json_decode(json_encode($id), true);
            return $msg ? response(['msg' => $msg, 'message' => '审批流添加成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '审批流添加失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法验证']);
        }

    }

    /**
     * 【审批流删除】
     * @Author 孙凯
     * @DateTime 2018-06-05
     * @param id int id
     * @param type string 类型
     * @param approval_id int 角色id
     * @param reset_person string 修改人
     * @param Request $request
     * @return array
     */
    public function del(Request $request)
    {
        $data=$request->all();
       // dd($data);
        $id = $data['id'];
            $msg=Db::table('approval')->where('id',$id)->delete();
            return $msg ? response(['msg' => $msg, 'message' => '审批流删除成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '审批流删除失败', 'code' => 1011], 400);

    }

    /**
     * 【审批流修改】
     * @Author 孙凯
     * @DateTime 2018-06-05
     * @param id int id
     * @param type string 类型
     * @param approval_id int 角色id
     * @param reset_person string 修改人
     * @param Request $request
     * @return array
     */
    public function save(Request $request)
    {
        $arr = $request->all();
//        dd($arr);
        $id = $arr['id'];
        if (empty($arr['type'])||empty($arr['approval_id'])||empty($arr['reset_person'])){
            return '参数不全';
        }
        try {
            $id = DB::table('approval')->where('id',$id)->update(
                [
                    'type' => $arr['type'],
                    'approval_id' => $arr['approval_id'],
                    'reset_person' => $arr['reset_person'],
                    'time' => time(),
                ]);
            $msg = json_encode($id);
            return $msg ? response(['msg' => $msg, 'message' => '审批流修改成功', 'code' => 1000], 200) : response(['msg' => '', 'message' => '审批流修改失败', 'code' => 1011], 400);
        } catch (JWTException $e) {
            return response(['msg' => '', 'code' => 1011, 'message' => '非法的token']);
        }
    }

}